/*
package com.duobaoyu.dby.supply.core.common.handler;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import com.duobaoyu.dby.supply.core.common.constant.StringPool;
import com.duobaoyu.dby.supply.core.common.util.DeflaterUtils;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

*/
/**
 * @author liyang
 * @version 1.0.0
 * @ClassName JsonZipTypeHandler
 * @Description 数据库JSON字符串压缩转对接TypeHandler
 * @createTime 08:54:00 2023/09/04
 *//*

@MappedTypes(value = {JSONObject.class})
@MappedJdbcTypes(value = {JdbcType.VARCHAR}, includeNullJdbcType = true)
public class JsonZipTypeHandler <T> extends BaseTypeHandler<T> {

    private static final ObjectMapper MAPPER;
    private Class<T> clazz;

    static {
        MAPPER = new ObjectMapper();
        // 反序列化设置 关闭反序列化时Jackson发现无法找到对应的对象字段，便会抛出UnrecognizedPropertyException: Unrecognized field xxx异常
        MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
        MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    }

    public JsonZipTypeHandler(Class<T> clazz) {
        if (Objects.isNull(clazz)) {
            throw new IllegalArgumentException("Type argument cannot be null");
        }
        this.clazz = clazz;
    }

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, this.toJson(parameter));
    }

    @Override
    public T getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return this.toObject(rs.getString(columnName), clazz);
    }

    @Override
    public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return this.toObject(rs.getString(columnIndex), clazz);
    }

    @Override
    public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return this.toObject(cs.getString(columnIndex), clazz);
    }

    private String toJson(T object) {
        try {
            String jsonStr = MAPPER.writeValueAsString(object);
            if(this.isCompress()) {
                String zipString = DeflaterUtils.zipString(jsonStr);
                return StringPool.BRACKET_START.concat(StringPool.QUOTE).concat(zipString).concat(StringPool.QUOTE).concat(StringPool.BRACKET_END);
            }
            return jsonStr;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private T toObject(String content, Class<T> clazz) {
        if (Objects.nonNull(content) && !content.isEmpty()) {
            try {
                String unzipString = content;
                if(content.startsWith(StringPool.BRACKET_START.concat(StringPool.QUOTE)) && content.endsWith(StringPool.QUOTE.concat(StringPool.BRACKET_END))) {
                    String jsonStr = StrUtil.strip(content, StringPool.BRACKET_START.concat(StringPool.QUOTE), StringPool.QUOTE.concat(StringPool.BRACKET_END));
                    if(!JSONUtil.isTypeJSON(jsonStr) && Base64.isBase64(jsonStr)) {
                        unzipString = DeflaterUtils.unzipString(jsonStr);
                    }
                }
                if(clazz.equals(List.class)){
                    return (T) MAPPER.readValue(unzipString,getJavaArrayType());
                }
                return (T)MAPPER.readValue(unzipString, clazz);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } else {
            return null;
        }
    }

    public JavaType getJavaArrayType() {
        return getCollectionType(ArrayList.class,Object.class);
    }


    protected JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
        return MAPPER.getTypeFactory().constructParametricType(collectionClass, elementClasses);
    }

    */
/**
     * 是否需要压缩
     * @return
     *//*

    protected Boolean isCompress() {
        return Boolean.TRUE;
    }
}
*/
